#include "../NewSyndrome-module.h"

void
module_init (void)
{
  bot_set_auteur_module ("Thaeron");
  bot_set_desc_module ("Check le nombre de users d'un channel");
  bot_set_ident_module ("nbusers");
  bot_set_version_module ("0.1");
  bot_add_type_handler ("join", "maj_nbu");
  bot_add_type_handler ("quit", "maj_nbu");
  bot_add_type_handler ("part", "maj_nbu");
  bot_add_type_handler ("kick", "maj_nbu");
}

#ifdef WIN32
void
init_dep (char *nom, void *addr)
{
  init_share_dep (nom, addr);
}
#endif


int
update_nbu (char *n, char *f)
{
  char **c;
  int i;
  FILE *fd;

  c = bot_get_userlist (n);
  if (!c)
      return (0);

  for (i = 0; c[i]; i++);
  free (c);

  fd = fopen (f, "w");
  if (!fd)
      return (0);

  fprintf (fd, "%d", i);
  fclose (fd);
  return (1);
}



int
maj_nbu (msg * i)
{
  int flag = -1;

  if (!strcmp (i->cmd, "quit"))
    flag = 0;
  if (flag == -1)
    {
      if (!strcasecmp (i->dest, "#gays"))
	flag = 1;

      if (!strcasecmp (i->dest, "#lesbiennes"))
	flag = 2;
    }

  if (flag == -1)
    return (0);

  if (!flag || flag == 1)
    update_nbu ("#gays", "../site_web/NewSyndrome/nbu/g.txt");

  if (!flag || flag == 2)
    update_nbu ("#lesbiennes", "../site_web/NewSyndrome/nbu/l.txt");

  return (0);
}
